package com.cleargroup.magic.cleara.c.command;

import com.cleargroup.magic.cleara.c.CpuUtil;

import java.util.ArrayList;
import java.util.List;


public class BatteryAppItem {

    ProcessItem mProcess;
    int weight;

    float cpu;
    long mLastAppCpuTime;
    long mLastTotalCpuTime;
    public List<IntentFilterInfo> mIntentFilters;

    public BatteryAppItem(ProcessItem process) {
        mProcess = process;
        mLastAppCpuTime = CpuUtil.getAppCpuTime(process.pid);
        mLastTotalCpuTime = CpuUtil.getTotalCpuTime();
        mIntentFilters = new ArrayList<>();
    }

    public float getCpu() {
        return cpu;
    }

    public float reloadCpu() {
        try {
            long currentCpuTime = 0;
            long currentTotalCpuTime = 0;
            if (mLastAppCpuTime != -1) {
                currentCpuTime = CpuUtil.getAppCpuTime(mProcess.pid);
                currentTotalCpuTime = CpuUtil.getTotalCpuTime();
            } else {
                currentCpuTime = -1;
                currentTotalCpuTime = 0;
            }


            if ((currentCpuTime >= 0 && mLastAppCpuTime >= 0) && (currentCpuTime - mLastAppCpuTime) >= 0 && (currentTotalCpuTime - mLastTotalCpuTime) > 0) {
                cpu = ((100L * (currentCpuTime - mLastAppCpuTime) * 100f / (currentTotalCpuTime - mLastTotalCpuTime) * 1.0f)) / 100f;
            } else {
                cpu = 0;
            }
            mLastAppCpuTime = currentCpuTime;
            mLastTotalCpuTime = currentTotalCpuTime;
            if (cpu > 100f) {
                cpu = 100f;
            }
        } catch (Exception e) {
        }
        return cpu;
    }

    void setChecked(boolean checked) {
        mProcess.isChecked = checked;
    }

    String getPkgName() {
        return mProcess.pkgName;
    }

    String getIconUri() {
        return mProcess.iconUri;
    }

    String getAppName() {
        return mProcess.appName;
    }

    long getRamUsage() {
        return mProcess.getRamUsage();
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

}
